import {
	defineStore
} from 'pinia'
import LoginApi from '@/api/login.js'
import HomeApi from '@/api/home.js'
export const useloginStore = defineStore({
	id: 'login', // id必填，且需要唯一
	state: () => {
		return {
			userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo') : {},
			globalConfig: uni.getStorageSync('globalConfig') ? uni.getStorageSync('globalConfig') : [],
			appSetting: uni.getStorageSync('appSetting') ? uni.getStorageSync('appSetting') : [],
			multiport: false
		}
	},
	// actions 用来修改 state
	actions: {
		login(userInfo) {
			uni.setStorageSync('userInfo', userInfo)
			this.userInfo = userInfo
		},
		logout() {
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync("authToken");
			this.userInfo = {};
			uni.reLaunch({
				url: "/pages/login/login"
			})
		},
		getGlobalConfig() {
			HomeApi.getconfigInfo().then((res) => {
				this.globalConfig = res.data;
				uni.setStorageSync('app_logo', res.data.site.logo)
				uni.setStorageSync('app_name', res.data.site.name)
				uni.setStorageSync('miniapp_audit', res.data.site.miniapp_audt_status)
				uni.setStorageSync('pay', res.data.pay)
				uni.setStorageSync('globalConfig', res.data)
				uni.setStorageSync('def_avatar', res.data.def.def_avatar)
				uni.setStorageSync('def_name', res.data.def.def_name)
				uni.setStorageSync('default_customer_headimg', res.data.def.default_customer_headimg)
				uni.setStorageSync('min_amount', res.data.def.min_amount)
				uni.setStorageSync('placard', res.data.wechat.placard)
				uni.setStorageSync('entryposter', res.data.wechat.entryposter)
				uni.setStorageSync('chat_key', res.data.gpt.key)
				uni.setStorageSync('chat_url', res.data.gpt.url)
				uni.setStorageSync('oss_domain', res.data.oss_domain)
			})
		},
		setAppSetting(setting) {
			uni.setStorageSync('appSetting', setting)
			this.appSetting = setting
		}
	}
})